import java.util.Scanner;

public class Demo4 {
    public static void main(String[] args) {
        Object locker = new Object();
        Thread t1 = new Thread(()->{
           synchronized (locker){
               System.out.println("t1等待之前");
               try {
                   locker.wait();
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
               System.out.println("t1等待之后");
           }
        });

        Thread t2 = new Thread(()->{
            Scanner sc = new Scanner(System.in);
            synchronized (locker){
                System.out.println("t2通知之前");
                //借助 scanner 控制阻塞,用户输入之前,都是阻塞状态
                sc.next();
                locker.notify();
                //将t1唤醒,t1不是立即就能执行的,t1要重新获取到锁
                //t2释放后,t1自动解除阻塞
                System.out.println("t2通知之后");
            }
        });

        t1.start();
        t2.start();
    }
}
